<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Database_Query_Mysql_Update extends Database_Query_Mysql {
	private $_table = '';
	private $_fields = array();
	private $_where = '';

	public function table($table) {
		$this->_table = $table;
		return $this;
	}

	public function field($field, $value, $type = 'string') {
		$elem = array();
		$elem['field'] = $field;
		$elem['value'] = $value;
		$elem['type'] = $type;
		$this->_fields[] = $elem;
		return $this;
	}
	
	public function where($where) {
		$this->_where = $where;
		return $this;
	}

	public function compile() {
		$sql = "UPDATE `" . $this->_table . "` SET ";

		$comma = false;
		foreach ($this->_fields as $field) {
			if ($comma) { $sql .= ','; }
			$sql .= "`" . $field['field'] . "`=" . $this->value($field['value'], $field['type']);
			$comma = true;
		}
		
		if (!empty($this->_where)) {
			$sql .= " WHERE (" . $this->_where . ")";
		}
		
		return $sql;
	}
}
